import numpy as np
import cPickle

Start = 7 
Stop = 8 

for i in range(Start,Stop+1):
    h = 1.0/(2**i)
    print 'h is', h
    erru1 = []
    erru2 = []
    erruinf = []
    errv1 = []
    errv2 = []
    errvinf = []
    errmu1 = []
    errmu2 = []
    errmuinf = []

    base = 'conv_' + str(i)
    print base
    in_str = base + 'in.pkl'
    out_str = base + 'out.pkl'
    pkl_file_in = open(in_str, 'rb')
    pkl_file_out = open(out_str, 'rb')

    pklist_in = cPickle.load(pkl_file_in)
    pklist_out = cPickle.load(pkl_file_out)

    N = len(pklist_in)/3
    print 'length is', N/3

    count = 0 
    for s,d in zip(pklist_in,pklist_out):
        #print s.shape
        #print d.shape
        m = np.abs(s).max()
        einf = np.abs(s-d).max()
        e1 = np.sum(np.abs(s-d))*h**2
        e2 = np.sqrt(np.sum(abs(s-d)**2)*h**2)
        #print 'magnitude is', m 
        #print 'error is', e 
        #print 'percent difference is', e/m*100
        if (count % 3) == 0:
            erruinf.append(einf) 
            erru1.append(e1) 
            erru2.append(e2) 
        elif (count % 3) == 1: 
            errvinf.append(einf) 
            errv1.append(e1) 
            errv2.append(e2) 
        else:
            errmuinf.append(einf) 
            errmu1.append(e1) 
            errmu2.append(e2) 
    
        count += 1 

    if (i > Start):
        for j in range(0, N):
            print 'u1 linf ratio is', erruinf[j]/erruinfold[j]  
            print 'u2 linf ratio is', errvinf[j]/errvinfold[j]  
            print 'mu1 linf ratio is', errmuinf[j]/errmuinfold[j]  
            print 'u1 l1 ratio is', erru1[j]/erru1old[j]  
            print 'u2 l1 ratio is', errv1[j]/errv1old[j]  
            print 'mu1 l1 ratio is', errmu1[j]/errmu1old[j]  
            print 'u1 l2 ratio is', erru2[j]/erru2old[j]  
            print 'u2 l2 ratio is', errv2[j]/errv2old[j]  
            print 'mu1 l2 ratio is', errmu2[j]/errmu2old[j]  

        for j in range(0, N):
            print 'u1 linf convergence rate is', -np.log(erruinf[j]/erruinfold[j])/np.log(2)  
            print 'u2 linf convergence rate is', -np.log(errvinf[j]/errvinfold[j])/np.log(2)  
            print 'mu1 linf convergence rate is', -np.log(errmuinf[j]/errmuinfold[j])/np.log(2)  
            print 'u1 l1 convergence rate is', -np.log(erru1[j]/erru1old[j])/np.log(2)  
            print 'u2 l1 convergence rate is', -np.log(errv1[j]/errv1old[j])/np.log(2)  
            print 'mu1 l1 convergence rate is', -np.log(errmu1[j]/errmu1old[j])/np.log(2)  
            print 'u1 l2 convergence rate is', -np.log(erru2[j]/erru2old[j])/np.log(2)  
            print 'u2 l2 convergence rate is', -np.log(errv2[j]/errv2old[j])/np.log(2)  
            print 'mu1 l2 convergence rate is', -np.log(errmu2[j]/errmu2old[j])/np.log(2)  

        print erru1old
        print erru1

    erru1old = erru1
    erru2old = erru2
    erruinfold = erruinf
    errv1old = errv1
    errv2old = errv2
    errvinfold = errvinf
    errmu1old = errmu1
    errmu2old = errmu2
    errmuinfold = errmuinf

